//
//  AppTools.m
//  Fish
//
//  Created by KING on 16/5/11.
//  Copyright © 2016年 huwo. All rights reserved.
//

#import "AppTools.h"


@implementation AppTools
+(void)postData:(NSString *)url from:(AFNData)data dic:(NSDictionary *)dic success:(AFNBlock)successBlock error:(AFNErrorBlock)errorblock{
    AFHTTPSessionManager *Manager = [AFHTTPSessionManager manager];
    
    Manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [Manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        data(formData);
       
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successBlock(responseObject);

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"s失败");
        errorblock(error);
        
        
    }];

}
+ (void)createAFN:(NSString *)url success:(AFNBlock)successBlock error:(AFNErrorBlock)errorblock{
   
    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [session GET:urlEncode parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败");
        errorblock(error);
        
    }];

}

//+ (void)HUDAlert:(id)view string:(NSString *)string{
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud.labelText = string;
//    hud.mode = MBProgressHUDModeText;
//    [hud hide:YES afterDelay:1];
//}
+(void)postURL:(NSString *)url data:(NSDictionary *)dic success:(AFNBlock)successBlock error:(AFNErrorBlock)errorblock{
//    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
//    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//    session.responseSerializer = [AFHTTPResponseSerializer serializer];
//    session.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [session POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorblock(error);
        
    }];
    

}

+(void)Alert:(NSString *)string s:(id)s{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    [s presentViewController:alertC animated:YES completion:^{
        
        
    }];
    
    UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [s dismissViewControllerAnimated:YES completion:^{

        }];
        
    }];
    [alertC addAction:alertA];
}




@end
